


/**
 * primitive Water 大量加载
 * 
 */
import Primitive from "../Primitive"
import normalMap from '../../materials/image/water/Water_2_M_Normal.jpg'

const DEFAULT_STYLE = {
  baseWaterColor: new Cesium.Color(72/255, 219/255, 251/255, .58),
  blendColor : new Cesium.Color(84/255, 160/255, 255/255, .58),
  normalMap: normalMap,
  animationSpeed: 0.01,
  amplitude:10,
  specularIntensity: 0.8,
  frequency: 200.0,
  animationSpeed: 0.009,
  amplitude: 10.0
}

export default class WaterPrimitive extends Primitive {
  /**
   * 
   * @param {*} position 
   * @param {*} options 
   */
  constructor(positions, options = {}) {
    super()
    this._positions = positions
    this._type = 'water_primitive'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }

    this.delegate = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: this.createGeometry()
      })
    })

    this.mounted()
  }

  /**
   * 初始化
   */
  mounted() {
    this.positions = this._positions
    this.style = this._style

    !this.delegate.appearance && this.setAppearance({
      type: 'Water',
      ...this._style  
    })
  }
  /**
   * 创建图形
   * @returns 
   */
  createGeometry() {
    let polygon = new Cesium.PolygonGeometry({
      polygonHierarchy: new Cesium.PolygonHierarchy(this._positions),

    })
    return Cesium.PolygonGeometry.createGeometry(polygon)
  }

  /**
   * 设置材质
   * @returns 
   */
   setAppearance(MaterialOption, options = {}) {
    if (!MaterialOption) return

    let type = MaterialOption.type
    delete MaterialOption.type
    this.delegate.appearance = new Cesium.EllipsoidSurfaceAppearance({
      material: Cesium.Material.fromType(type, {
        ...MaterialOption
      }),
      ...options
    })
  }
  

  /**
   * 设置geometry样式
   * @param {*} style 
   * @returns 
   */
  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  get positions() {
    return this._positions
  }

  set positions(val) {
    this._positions = val
    this.delegate.geometryInstances.geometry.PolygonHierarchy = new Cesium.PolygonHierarchy(this._positions)
  }
}
